<!-- 编辑弹窗 -->
<template>
  <ele-modal
    :form="true"
    :destroy-on-close="true"
    width="70%"
    v-model="visible"
    :title="isUpdate ? '修改' : '新建'"
  >
    <div class="example">编码示例</div>
<<<<<<< HEAD
    <div class="code-example">
      <div class="value">
        {{ form.date2 || '' }}
      </div>
      <div class="tag">
        {{ form.deptName }}
      </div>
    </div>
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      :rules="rules"
      label-width="120px"
      @submit.prevent=""
    >
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item
            label="编码规则编号"
            prop="deptCode"
            style="width: 100%"
          >
            <el-input
              clearable
              v-model="form.deptCode"
              placeholder="请输入编码规则编号"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="编码规则名称"
            prop="deptName"
            style="width: 100%"
          >
            <el-input
              clearable
              v-model="form.deptName"
              placeholder="请输入编码规则名称"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12" :lg="8">
          <el-form-item label="常量" prop="nodeType" style="width: 100%">
            <el-radio-group v-model="form.nodeType">
              <el-radio value="M" label="启用" />
              <el-radio value="C" label="不启用" />
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12" :lg="8">
          <el-form-item label="常量值" prop="deptName" style="width: 100%">
            <el-input clearable v-model="form.deptName" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12" :lg="8">
          <el-form-item label="尾部连续符" prop="symbol" style="width: 100%">
            <el-radio-group v-model="form.symbol">
              <el-radio value="M" label="无" />
              <el-radio value="C" label="横杠" />
              <el-radio value="D" label="下划线" />
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12" :lg="8">
          <el-form-item label="日期" prop="date1" style="width: 100%">
            <el-radio-group v-model="form.date1">
              <el-radio value="M" label="启用" />
              <el-radio value="C" label="不启用" />
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12" :lg="8">
          <el-form-item label="日期结构" prop="date2" style="width: 100%">
            <el-select
              v-model="form.date2"
              placeholder="请选择"
              style="width: 196px"
            >
              <el-option label="长年份YYYY" value="A" />
              <el-option label="短年份YY" value="B" />
              <el-option label="长月份YYYYMM" value="C" />
              <el-option label="短月份YYMM" value="D" />
              <el-option label="长日期YYYYMMDD" value="E" />
              <el-option label="短日期YYMMDD" value="F" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" :lg="8">
          <el-form-item
            label="尾部连续符"
            prop="dateSymbol"
            style="width: 100%"
          >
            <el-radio-group v-model="form.dateSymbol">
              <el-radio value="M" label="无" />
              <el-radio value="C" label="横杠" />
              <el-radio value="D" label="下划线" />
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12" :lg="8">
          <el-form-item label="流水号" prop="liushuihao" style="width: 100%">
            <el-link
              v-permission="'system:dept:add'"
              type="primary"
              underline="never"
              @click="openEdit(null, row.deptId)"
            >
              启用
            </el-link>
          </el-form-item>
        </el-col>
        <el-col :span="12" :lg="8">
          <el-form-item label="起始值" prop="orderNum" style="width: 100%">
            <el-input-number
              :min="0"
              :max="99999"
              v-model="form.orderNum"
              placeholder="请输入显示排序"
              controls-position="right"
              style="width: 200px"
              class="ele-fluid"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12" :lg="8">
          <el-form-item label="长度" prop="orderNum" style="width: 100%">
            <el-input-number
              :min="0"
              :max="99999"
              v-model="form.orderNum"
              placeholder="请输入显示排序"
              controls-position="right"
              style="width: 200px"
              class="ele-fluid"
            />
          </el-form-item>
        </el-col>
      </el-row>
=======
    <el-form ref="formRef" :inline="true" :model="form" :rules="rules" label-width="120px" @submit.prevent="">
      <el-form-item label="编码规则编号" prop="deptCode" style="width:45%">
        <el-input clearable v-model="form.deptCode" placeholder="请输入编码规则编号" />
      </el-form-item>
      <el-form-item label="编码规则名称" prop="deptName" style="width:45%">
        <el-input clearable v-model="form.deptName" placeholder="请输入编码规则名称" />
      </el-form-item>
      <el-form-item label="常量" prop="nodeType">
        <el-radio-group v-model="form.nodeType">
          <el-radio value="M" label="启用" />
          <el-radio value="C" label="不启用" />
        </el-radio-group>
      </el-form-item>
      <el-form-item label="常量值" prop="deptName">
        <el-input clearable v-model="form.deptName" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="尾部连续符" prop="symbol">
        <el-radio-group v-model="form.symbol">
          <el-radio value="M" label="无" />
          <el-radio value="C" label="横杠" />
          <el-radio value="D" label="下划线" />
        </el-radio-group>
      </el-form-item>
      <el-form-item label="日期" prop="date1">
        <el-radio-group v-model="form.date1">
          <el-radio value="M" label="启用" />
          <el-radio value="C" label="不启用" />
        </el-radio-group>
      </el-form-item>
      <el-form-item label="日期结构" prop="date2">
        <el-select v-model="form.date2" placeholder="请选择" style="width:196px">
          <el-option label="长年份YYYY" value="A" />
          <el-option label="短年份YY" value="B" />
          <el-option label="长月份YYYYMM" value="C" />
          <el-option label="短月份YYMM" value="D" />
          <el-option label="长日期YYYYMMDD" value="E" />
          <el-option label="短日期YYMMDD" value="F" />
        </el-select>

      </el-form-item>
      <el-form-item label="尾部连续符" prop="dateSymbol">
        <el-radio-group v-model="form.dateSymbol">
          <el-radio value="M" label="无" />
          <el-radio value="C" label="横杠" />
          <el-radio value="D" label="下划线" />
        </el-radio-group>
      </el-form-item>

      <el-form-item label="流水号" prop="">

        <el-link v-permission="'system:dept:add'" type="primary" underline="never" @click="openEdit(null, row.deptId)">
          启用
        </el-link>
      </el-form-item>

      <el-form-item label="起始值" prop="orderNum" label-width="225">
        <el-input-number :min="0" :max="99999" v-model="form.orderNum" placeholder="请输入显示排序" controls-position="right"
          style="width: 200px" class="ele-fluid" />
      </el-form-item>
      <el-form-item label="长度" prop="orderNum">
        <el-input-number :min="0" :max="99999" v-model="form.orderNum" placeholder="请输入显示排序" controls-position="right"
          style="width: 200px" class="ele-fluid" />
      </el-form-item>
>>>>>>> 0df5d4a558504b435b17d749b30f9ba5632f8c78
    </el-form>

    <template #footer>
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script setup>
  import { ref, reactive, watch } from 'vue';
  import { EleMessage, emailReg, phoneReg } from 'ele-admin-plus';
  import { useFormData } from '@/utils/use-form-data';
  import EncodeSelect from './encode-select.vue';
  import { addDept, updateDept } from '@/api/system/dept';

  const props = defineProps({
    /** 修改回显的数据 */
    data: Object,
    /** 上级部门id */
    parentId: Number,
    /** 指定机构下拉数据 */
    organizationData: Array
  });

  const emit = defineEmits(['done']);

  /** 弹窗是否打开 */
  const visible = defineModel({ type: Boolean });

  /** 是否是修改 */
  const isUpdate = ref(false);

  /** 提交状态 */
  const loading = ref(false);

  /** 表单实例 */
  const formRef = ref(null);

  /** 表单数据 */
  const [form, resetFields, assignFields] = useFormData({
    deptId: void 0,
    parentId: void 0,
    deptName: '',
    orderNum: 0,
    leader: '',
    phone: '',
    email: '',
    status: '0',
    date1: '',
    date2: '',
    symbol: '',
    nodeType: ''
  });

  /** 表单验证规则 */
  const rules = reactive({
    deptCode: [
      {
        required: true,
        message: '请输入',
        type: 'string',
        trigger: 'blur'
      }
    ],
    deptName: [
      {
        required: true,
        message: '请输入',
        type: 'string',
        trigger: 'blur'
      }
    ],
    date1: [
      {
        required: true,
        message: '请选择',
        trigger: 'change'
      }
    ],
    date2: [
      {
        required: true,
        message: '请选择',
        trigger: 'change'
      }
    ],
    dateSymbol: [
      {
        required: true,
        message: '请选择',
        trigger: 'change'
      }
    ],
    nodeType: [
      {
        required: true,
        message: '请选择',
        trigger: 'change'
      }
    ],
    symbol: [
      {
        required: true,
        message: '请选择',
        trigger: 'change'
      }
    ],
    orderNum: [
      {
        required: true,
        message: '请输入',
        type: 'number',
        trigger: 'blur'
      }
    ],
    phone: [
      {
        pattern: phoneReg,
        message: '手机号格式不正确',
        type: 'string',
        trigger: 'blur'
      }
    ],
    email: [
      {
        pattern: emailReg,
        message: '邮箱格式不正确',
        type: 'string',
        trigger: 'blur'
      }
    ]
  });

  /** 关闭弹窗 */
  const handleCancel = () => {
    visible.value = false;
  };

  /** 保存编辑 */
  const save = () => {
    formRef.value?.validate?.((valid) => {
      if (!valid) {
        return;
      }
      loading.value = true;
      const saveOrUpdate = isUpdate.value ? updateDept : addDept;
      saveOrUpdate({ ...form, parentId: form.parentId || 0 })
        .then((msg) => {
          loading.value = false;
          EleMessage.success({ message: msg, plain: true });
          handleCancel();
          emit('done');
        })
        .catch((e) => {
          loading.value = false;
          EleMessage.error({ message: e.message, plain: true });
        });
    });
  };

  /** 监听弹窗打开 */
  watch(visible, () => {
    if (visible.value) {
      if (props.data) {
        assignFields({
          ...props.data,
          parentId: props.data.parentId || void 0
        });
        isUpdate.value = true;
      } else {
        resetFields();
        form.parentId = props.parentId;
        isUpdate.value = false;
      }
    }
  });
</script>
<style scoped>
<<<<<<< HEAD
  .example {
    background: #e4e4e4;
    padding: 10px;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
  }
  .data-show {
    height: 100px;
  }
  .code-example {
    margin-bottom: 20px;
    text-align: center;
    background: #e4e4e4;
    /* background-color: rgb(246, 246, 246); */
    padding: 12px 0;
    color: #000;
    .title {
      font-weight: 700;
      margin-right: 20px;
      padding-left: 30px;
    }
    .value {
      font-size: 25px;
      font-weight: 700;
    }
  }
=======
.example {
  margin-bottom: 10px;
  background: #e4e4e4;
  padding: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}
.data-show{
  height: 100px;

}
.code-example {
		text-align: center;
		/* background-color: rgb(246, 246, 246); */
		padding: 12px 0;
		color: #000;
		.title {
			font-weight: 700;
			margin-right: 20px;
			padding-left: 30px;
		}
		.value {
			font-size: 25px;
			font-weight: 700;
		}
	}
>>>>>>> 0df5d4a558504b435b17d749b30f9ba5632f8c78
</style>
